package com.shd.edanpei.presenter;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.util.contact.ContactBean;
import com.shd.edanpei.presenter.contract.ContactsContract;
import com.shd.edanpei.util.GsonUtil;
import com.shd.edanpei.util.LayoutUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/9/4.
 * yang-wen-wen
 */

public class ContactsPresenter extends BasePresenterImpl<ContactsContract.ContactsIView>
        implements ContactsContract.ContactsIPresenter {
;
    private Set<ContactBean> set = new HashSet<>();
    private List<ContactBean> contactList = new ArrayList<>();
    private List<ContactBean> serachList = new ArrayList<>();

    public ContactsPresenter(ContactsContract.ContactsIView mView) {
        super(mView);
    }

    @Override
    public void getContacts() {
        mBaseIView.showLoading("");
        ApiCreator.getApi().friendsList()
                .compose(RxTransformer.<List<ContactBean>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<List<ContactBean>>() {
                    @Override
                    protected void onSuccess(List<ContactBean> data) {
                        if (data != null && data.size() > 0) {
                            mBaseIView.getAdapter().setNewData(data);
                            contactList = data;
                        } else {
                            mBaseIView.getAdapter().setNewData(null);
                            mBaseIView.getAdapter()
                                    .setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.pic_noperson, "暂无联系人"));
                        }
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getAdapter()
                                .setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.pic_noperson, "暂无联系人"));
                    }

                    @Override
                    public void onFinish() {
                        mBaseIView.stopLoading();
                    }
                });
    }

    @Override
    public void leadContracts(List<ContactBean> list) {
        set.addAll(contactList);
        for (ContactBean contactBean : list) {
            set.add(contactBean);
        }
        contactList = new ArrayList<>(set);
        //上传通讯录
        uploadContacts();
        mBaseIView.getAdapter().setNewData(contactList);
    }

    @Override
    public void toSearch(String str) {
        serachList.clear();
        for (ContactBean contactBean : contactList) {
            if (contactBean.toString().contains(str.replace(" ", ""))) {
                serachList.add(contactBean);
            }
        }
        mBaseIView.setSearchData(serachList);
    }

    private void uploadContacts() {
        if (contactList.size() > 0) {
            ApiCreator.getApi().uploadContacts(GsonUtil.toJson(contactList))
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {

                        }
                    });
        }
    }
}
